package com.wehgu.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wehgu.admin.common.BaseController;
import com.wehgu.admin.common.Constant;
import com.wehgu.admin.common.ResultTemplate;
import com.wehgu.admin.common.snowId.IdCommon;
import com.wehgu.admin.common.vo.UserInfoVO;
import com.wehgu.admin.entities.SysUser;
import com.wehgu.admin.entities.SysUserDetail;
import com.wehgu.admin.entities.dto.UserPageQuery;
import com.wehgu.admin.entities.dto.UserRegisterParam;
import com.wehgu.admin.service.ISysUserDetailService;
import com.wehgu.admin.service.ISysUserService;
import com.wehgu.admin.utils.EmptyUtil;
import com.wehgu.admin.utils.PasswordUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**
 * <p> 首页 </p>
 *
 */
@Api(tags = "首页-接口")
@RestController
public class IndexController extends BaseController {
    @Resource
    private ISysUserService iSysUserService;

    @Resource
    private ISysUserDetailService iSysUserDetailService;

    @Resource
    private IdCommon idCommon;


    @PostMapping(value = "/login", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "登录系统", httpMethod = "POST", response = ResultTemplate.class)
    public void login(@RequestBody String input){}


    @PostMapping(value = "/register", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "用户注册", httpMethod = "POST", response = ResultTemplate.class, notes = "用户注册")
    public ResultTemplate userRegister(@RequestBody UserRegisterParam input) {
        EmptyUtil.isEmpty(input.getTelephone(), "请填写手机号");
        EmptyUtil.isEmpty(input.getPassword(), "请填写密码");

        //验证是否存在用户
        SysUser one = iSysUserService.getOne(new QueryWrapper<SysUser>()
                .eq("telephone", input.getTelephone())
                .last("LIMIT 1"));

        if(one==null){
            //验证密码强度
            EmptyUtil.bool(PasswordUtil.isLetterDigit(input.getPassword()), "密码需包含大小写字母及数字且在8-20位");

            if(input.getRoleUID()==null||input.getRoleUID().equals("")){
                input.setRoleUID(Constant.MP_USER);
            }

            if(input.getRoleUID()==null||input.getRoleUID().equals("")){
                input.setRoleUID(Constant.MP_USER);
            }

            if(input.getDeptUID()==null||input.getRoleUID().equals("")){
                input.setDeptUID(Constant.MP_DEFAULT_DEPT);
            }

            if(input.getUnionId()==null||input.getRoleUID().equals("")){
                input.setUnionId("");
            }

            String UserUID= UUID.randomUUID().toString();
            String UserDetailUID= UUID.randomUUID().toString();
            SysUser userInfo=new SysUser();
            SysUserDetail accountInfo=new SysUserDetail();
            //保存用户信息
            userInfo.setUserUuid(UserUID);
            userInfo.setTelephone(input.getTelephone());

            EmptyUtil.bool(iSysUserService.save(userInfo), "注册失败");
            //保存账号信息 加密密码存储到数据库
            accountInfo.setPassword(PasswordUtil.encodePassword(input.getPassword()));
            accountInfo.setUserUuid(UserUID);
            accountInfo.setUserDetailUuid(UserDetailUID);
            accountInfo.setRoleUuid(input.getRoleUID());
            accountInfo.setDeptUuid(input.getDeptUID());
            accountInfo.setUnionId(input.getUnionId());

            boolean flag=iSysUserDetailService.save(accountInfo);
            if(!flag){
                iSysUserService.remove(new QueryWrapper<SysUser>()
                        .eq("user_uuid", userInfo.getUserUuid())
                        .last("LIMIT 1"));
                return ResultTemplate.err(400,"账号注册失败,请重新注册");
            }

        }else{
            return ResultTemplate.err(400,"手机号已被注册，请直接登录");
        }


        return ResultTemplate.ok(200,"注册成功");
    }



}
